#
# Restore Z-Mail 3.2 Motif menu layout as closely as possible in Z-Mail 3.3
#

if ! is_gui
    return
endif

function resend() {
    #% (fallback help text)
    #     resend [msg-list] [addresses]
    #
    # Resend (unedited) the listed messages to the addresses.
    #%
       # Versions earlier than 2.1 do not support "mail -resend".
       # Use equivalent backwards-compatible options just in case.
       mail -send -forward $*
}
function saveit() {
    #% (fallback help text)
    #      saveit [msg-list]
    #%
       if "x$_save_file" == "x"
           eval ask -f __save_file -d +/. "$__ask_saving"
       else
           ask -f __save_file -d "$__save_file" "$__ask_saving"
       endif
       if $status == 0
           save $* "$__save_file"
       endif
}
function bugreport() {
    #% (fallback help text)
    # Use this function to send mail to report problems with Z-Mail.
    #%
       mail -p bugs		# mail using prepared bugreport form
}
function view_msg() {
    #% (fallback help text)
    #      view_msg [folder]
    #
    # This is used to view a single rfc822 message in a file
    #%
    
       if $# == 1
           open -T "$1"
           if $status == 0
               pinup ^
           else
               return $status
           endif
       else
           eval error "$__err_takes_one_arg"
       endif
       return 0
}
function view_fldr() {
    #% (fallback help text)
    #      view_fldr [folder]
    #
    # This is used to view a folder (file of more than one message)
    #%
       if $# != 1
           eval error "$__err_takes_one_arg"
           return 0 # Suppress attachment error dialog
       endif
       ask -m -i __how -d "$__choice_merge" "$__ask_show_folder" \
           "$__choice_create" "$__choice_temp"
       if $status != 0
           return 0
       endif
       unset __list
       if "$__how" == "$__choice_merge"
           merge "$1" | set __list
           if $status != 0
               return 0
           endif
       else
           open -T "$1" | set __list
           if $status != 0
               return 0
           endif
           if ! $?__list
               close -n "$1"
               return 0
           endif
           if "$__how" == "$__choice_create"
               ask -f __vf_folder "$__ask_folder_write"
               if $status == 0
                   copy * $__vf_folder
               endif
               close -n "$1"
               unset __list
               open $__vf_folder | set __list
               if $status != 0
                   return 0
               endif
           endif
           ask -l __list -d 1 "$__ask_show_message" $__list
           if $status == 0
               pinup $__list
           endif
       endif
       return 0
}
function page_edit() {
    #% (fallback help text)
    #      page_edit [filename]
    #
    # This is used to create a new text file
    #%
       sh touch $1
       page -e $1
}
function lookup() {
    if ! $?address_book
        error "$__err_no_addrbook"
        return
    endif
    if is_gui
        dialog Browser
        return
    endif
    if is_lite
        dialog Browser
        return
    endif
    if $?1
        __lookup_pattern = "$*"
    else
        __lookup_pattern = "$__default_lookup_pattern"
    endif
    sh $address_book -1 "$__lookup_pattern"
}
function window_sh() {
    #% (fallback help text)
    #      window_sh command args ...
    #
    # Run an external command, in its own window if appropriate.  Note that
    # this forces use of a terminal emulator, even if $window_shell is empty.
    #%
       # Use of "$1" "$2" etc. works around unavailability of "$@" for now.
       if is_lite
           builtin screencmd -p builtin sh "$1" "$2" "$3" "$4" "$5" "$6" "$7" \
               "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15" "$16" "$17" \
               "$18" "$19" "$20" "$21" "$22" "$23" "$24" "$25"
           return $status
       endif
       if is_gui
           if $?window_shell
               set __window_shell = "$window_shell"
           else
               set __window_shell
           endif
           if X"$__window_shell" == X
               set __winterm
               if $?winterm
                   if X"$winterm" != X
                       set __winterm = "$winterm"
                   endif
               endif
               if X"$__winterm" != X
                   set __window_shell = "$__winterm -e"
               else
                   set __window_shell = "xterm -e"
               endif
           endif
           builtin sh $__window_shell "$1" "$2" "$3" "$4" "$5" "$6" "$7" \
               "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15" "$16" "$17" \
               "$18" "$19" "$20" "$21" "$22" "$23" "$24" "$25"
           return $status
       endif
       builtin sh "$1" "$2" "$3" "$4" "$5" "$6" "$7" \
           "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15" "$16" "$17" \
           "$18" "$19" "$20" "$21" "$22" "$23" "$24" "$25"
       return $status
}
function zmfilter_reply_each() {
    #% (fallback help text)
    #      zmfilter_reply_each [filename]
    #
    # This is used to reply to a message using a template file.  The string
    # $SUBJECT in the template file is replaced with the message's subject.
    #%
       setenv SUBJECT "$[%s]"
       sh "sed 's/"'\$SUBJECT'"/'"'"$SUBJECT"/'" $1 > /tmp/reply$$"
       unsetenv SUBJECT
       if $?autoinclude
           set __need_autoinclude
           unset autoinclude
       endif
       reply -unedited -draft /tmp/reply$$
       if $?__need_autoinclude
           set autoinclude
           unset __need_autoinclude
       endif
}
function zmfilter_reply() {
        foreach m $input 'msg_list - $m ; zmfilter_reply_each $1'
}
function create_aiff() {
    #% (fallback help text)
    #      create_aiff [recordaiff arguments]
    #
    # This function is used to create new audio attachments by recording
    # from the microphone.
    #%
       sh -m 'Activating microphone' apanel -nodisplay -nofork -source mic
       sh -m 'Recording ... press <Stop> when done.' recordaiff $*
    
       if $?child
           sh kill -INT $child
       endif
}
function zmenu_fwd_add_comments() {
    ask -i __comment "$__ask_add_comment"
    if $status == 0
        mail -C "$__comment" -resend
        unset __comment
    endif
}
function zmenu_send_and_close() {
    compcmd send
    if $status == 0
        dialog -close
    endif
}
function zmenu_insert_file() {
    ask -f __file "$__ask_file_read"
    if $status == 0
        if $1 == replace
            textedit set-item compose-body
            textedit delete-all
        endif
        compcmd insert-file "$__file"
        unset __file
    endif
}
function zmenu_insert_indented_file() {
    ask -f __file "$__ask_file_read"
    if $status == 0
        set __pfx=''
        if $?indent_str != 0
            set __pfx="$indent_str"
        endif
        if "x$__pfx" == "x"
            ask -i __pfx "$__ask_indent_with"
        else
            ask -i __pfx -d "$__pfx" "$__ask_indent_with"
        endif
        if $status == 0
            if $1 == replace
                textedit set-item compose-body
                textedit delete-all
            endif
            textedit set-item compose-body
            textedit get-cursor-position __indentstart
            compcmd insert-file "$__file"
            textedit get-cursor-position __indentend
            textedit set-selection-position $__indentstart $__indentend
            textedit indent "$__pfx"
        endif
        unset __file __pfx
    endif
}
function clear_output() {
    textedit set-item output-text
    textedit delete-all
}
function zmenu_save_to_file() {
    if "x$__save_file" != "x"
        ask -f __save_file -d "$__save_file" "$__ask_file_write"
    else
        ask -f __save_file "$__ask_file_write"
    endif
    if $status == 0
        compcmd save-to-file "$__save_file"
        if $status == 0
            eval echo "$__info_saved_to"
        endif
    endif
}
function zmenu_save_draft() {
    if "x$__draft_file" != "x"
        ask -f __draft_file -d "$__draft_file" "$__ask_draft_write"
    else
        ask -f __draft_file "$__ask_draft_write"
    endif
    if $status == 0
        compcmd save-draft "$__draft_file"
        if $status == 0
            eval echo "$__info_saved_draft"
        endif
    endif
}
function zmenu_next_ref_subj() {
    msg_list . | set __msgno
    set __subj="$[%S]"
    if "x$__subj" == "x"
        eval error $__err_no_subject
        unset __msgno __subj
        return
    endif
    search +2 -1 -r .-$ -s -n -e "$__subj"$
    if "x$output" == "x"
        eval error "$__err_no_next_about"
    else
        if "$output" == "$__msgno"
            eval error "$__err_no_next_about"
            unset __msgno __subj
        else
            msg_list - $output | display
        endif
    endif
    # unset __msgno __subj
}
function zmenu_prev_ref_subj() {
    msg_list . | set __msgno
    set __subj="$[%S]"
    if "x$__subj" == "x"
        eval error "$__err_no_subject"
        unset __msgno __subj
        return
    endif
    search -2 +1 -r ^-. -s -n -e "$__subj"$
    if "x$output" == "x"
        eval error "$__err_no_prev_about"
    else
        if $output == $__msgno
            eval error "$__err_no_prev_about"
        else
            msg_list - $output | display
        endif
    endif
    # unset __msgno __subj
}
function zmenu_all_ref_subj() {
    msg_list . | set __msgno
    if "x$[%S]" == "x"
        eval error "$__err_no_subject"
        unset __msgno
        return
    endif
    unset __msgno
    search -s -n -e "$[%S]"$
}
function zmenu_next_ref_auth() {
    msg_list . | set __msgno
    set __auth="$[%a]"
    search +2 -1 -r .-$ -n -f "$__auth"
    if $output == $__msgno
        eval error "$__err_no_next_from"
    else
        msg_list - $output | display
    endif
    # unset __msgno __auth
}
function zmenu_prev_ref_auth() {
    msg_list . | set __msgno
    set __auth="$[%a]"
    search -2 +1 -r ^-. -n -f "$__auth"
    if $output == $__msgno
        eval error "$__err_no_prev_from"
    else
        msg_list - $output | display
    endif
    # unset __msgno __auth
}
function zmenu_all_ref_auth() {
        search -n -f "$[%a]"
}
function zmenu_next_ref_msgid() {
    msg_list . | set __msgno
    set __mid="$[%i]"
    if "x$__mid" == "x"
        eval error "$__err_no_message_id"
        unset __msgno __mid
        return
    endif
    search +1 -r .-$ -h references,in-reply-to -n -e "$__mid"
    if "x$output" == x
        error "$__err_no_next_ref"
    else
        msg_list - $output | display
    endif
    # unset __msgno __mid
}
function zmenu_prev_ref_msgid() {
    if "x$[%?references?]" == x
        error "$__err_no_prev_ref"
        return
    endif
    set __msgidlist=
    foreach i ($[%?references?]) 'set __msgidlist += $i"\|"'
    set __msgidlist += NONEXISTENT
    search -1 -r ^-. -h message-id "$__msgidlist"
    if "x$output" == x
        error "$__err_no_prev_ref"
    else
        msg_list - $output | display
    endif
}
function zmenu_all_ref_msgid() {
    msg_list . | set __msgno
    set __mid="$[%i]"
    if "x$__mid" == "x"
        eval error "$__err_no_message_id"
        unset __msgno __mid
        return
    endif
    unset __msgno __mid
    search -n -h references,in-reply-to -e "$[%i]"
    msg_list . $output
}
function zmenu_all_ref_thread() {
    set __mid="$[%i]" __ref="$[%?references?]"
    if "x$__mid" != "x"
        set __msgidlist="$__mid"'\|'
    endif
    if x"$__ref" != x
        foreach i ($[%?references?]) 'set __msgidlist += $i"\|"'
    endif
    set __msgidlist += NONEXISTENT
    if "$__msgidlist" == NONEXISTENT
        eval error "$__err_no_message_id"
        unset __ref __mid __msgidlist
        return
    endif
    search -h message-id,references "$__msgidlist"
}
function zmenu_all_ref_mark() {
        msg_list `:m`
}
function zmenu_next_ref_mark() {
    msg_list .-$ {.} | :m
    if "x$output" == x
        error "$__err_no_next_mark"
    else
        search +1 -r $output | display
    endif
}
function zmenu_prev_ref_mark() {
    msg_list ^-. {.} | :m
    if "x$output" == x
        error "$__err_no_prev_mark"
    else
        search -1 -r $output | display
    endif
}
function zmenu_next_ref_priority() {
    if "x$[%?priority?]" == "x"
        msg_list . | set __msgno
        eval error "$__err_no_priority"
        unset __msgno
        return
    endif
    set __pri="$[%?priority?]"
    search -r .-$ {.} -p $__pri +1
    if "x$output" == x
        eval error "$__err_no_next_priority"
    else
        display $output
    endif
}
function zmenu_prev_ref_priority() {
    if "x$[%?priority?]" == "x"
        msg_list . | set __msgno
        eval error "$__err_no_priority"
        unset __msgno
        return
    endif
    set __pri="$[%?priority?]"
    search -r ^-. {.} -p $__pri -1
    if "x$output" == x
        eval error "$__err_no_prev_priority"
    else
        display $output
    endif
}
function zmenu_all_ref_priority() {
    if "x$[%?priority?]" == "x"
        msg_list . | set __msgno
        eval error "$__err_no_priority"
        unset __msgno
        return
    endif
    search -p $[%?priority?]
}
function zmenu_msg_pri_other() {
    ask -input __pri "$__ask_pri_other"
    if $status == 0
        mark -"$__pri"
    endif
}
function zmenu_comp_pri_other() {
    ask -input __pri "$__ask_pri_other"
    if $status == 0
        set compose_state += pri_"$__pri"
    endif
}
function zmenu_comp_body_textedit() {
    textedit set-item compose-body
    textedit $*
}
function zmenu_comp_body_textcopy() {
    textedit set-item compose-body
    textedit get-selection-position __start __end
    if $__start == $__end
        if ! $?compose_state:(edit_headers)
            error -p "$__err_no_selection"
        else
            zmenu_comp_header_textcopy $*
        endif
    else
        textedit $*
    endif
}
function zmenu_comp_header_textcopy() {
    textedit set-item compose-header
    textedit get-selection-position __start __end
    if $__start == $__end
        error -p "$__err_no_selection"
    else
        textedit $*
    endif
}
function zmenu_msg_body_textedit() {
    textedit set-item message-body
    textedit $*
}
function zmenu_msg_body_textcopy() {
    textedit set-item message-body
    textedit get-selection-position __start __end
    if $__start == $__end
        zmenu_msg_header_textcopy $*
    else
        textedit $*
    endif
}
function zmenu_msg_header_textcopy() {
    textedit set-item message-header
    textedit get-selection-position __start __end
    if $__start == $__end
        error -p "$__err_no_selection"
    else
        textedit $*
    endif
}
function zmenu_comp_pipe() {
    textedit set-item compose-body
    textedit get-selection-position __start __end
    if "x$__pipe_cmd" != "x"
        ask -i __pipe_cmd -d "$__pipe_cmd" "$__ask_pipe_to_shell"
    else
        ask -i __pipe_cmd "$__ask_pipe_to_shell"
    endif
    if $status == 0
        #if $__start == $__end
        #    textedit select-all
        #else
        textedit set-selection-position $__start $__end
        #endif
        textedit set-item compose-body
        textedit pipe "$__pipe_cmd"
    endif
}
function zmenu_comp_indent() {
    set __pfx=''
    textedit set-item compose-body
    textedit get-selection-position __indentstart __indentend
    if $?indent_str != 0
        eval -h set __pfx="'$indent_str'"
    endif
    if "x$__pfx" == "x"
        ask -i __pfx "$__ask_indent_with"
    else
        ask -i __pfx -d "$__pfx" "$__ask_indent_with"
    endif
    if $status == 0
        textedit set-selection-position $__indentstart $__indentend
        textedit indent "$__pfx"
        unset __pfx
    endif
}
function zmenu_comp_done() {
    if $?compose_state:(active) == 0
        dialog -close
        return
    endif
    ask -m -d "$__choice_cc_send" -i __action "$__ask_comp_cancel" "$__choice_cc_continue" "$__choice_cc_discard" "$__choice_cc_send"
    if $status == 0
        if "x$__action" == "x$__choice_cc_send"
            compcmd send
        endif
        if "x$__action" == "x$__choice_cc_discard"
            compcmd quit
        endif
        # Used "Done".  If not autodismiss, close window anyway.
        if $?autodismiss
            # Check for either set to nothing, or set to "compose"
            # Have to do this so we don't close the wrong dialog!
            if $?autodismiss:(compose)
                return 0
            endif
            if X"$autodismiss" == X
                return 0
            endif
        endif
        if $?compose_state:(active) == 0
            dialog -close
        endif
    endif
}
function zmenu_set_tmpdir() {
    unset __tmpdir
    if $?tmpdir
        if "x$tmpdir" != x
            set __tmpdir = "$tmpdir"
            return
        endif
    endif
    set __tmpdir = /tmp
}
function zmenu_comp_print() {
    zmenu_set_tmpdir
    ## To print both message and headers:
    # if $compose_state:(edit_headers)
    #    textedit set-item compose-header
    #    textedit save-to-file $__tmpdir/zmsg$$	# Write headers
    #    compcmd save-to-file $__tmpdir/zmsg$$	# Append body
    # else
    compcmd write-to-file $__tmpdir/zmsg$$		# Write body
    # endif
    ##
    if $status != 0
        return
    endif
    lpr $__tmpdir/zmsg$$
    remove -f $__tmpdir/zmsg$$
}
function zmenu_toolbar_include() {
    if !$compose_state:(active)
        error "$__err_no_compose"
        return
    endif
    ask -m -i __userChoice "$__ask_include" "$__choice_message" "$__choice_file"
    if $status != 0
        return -1
    endif
    if "$__userChoice" == "$__choice_file"
        ask -f __userChoice2 "$__ask_file_include"
        if $status != 0
            return -1
        endif
        compcmd insert-file "$__userChoice2"
    else
        compcmd include-message
    endif
}
function zmenu_toolbar_send() {
    if $?compose_state:(active)
        compcmd send
    else
        error "$__err_no_compose"
    endif
}
function zmenu_include_message_at_cursor() {
    textedit set-item compose-body
    textedit text-deselect
    textedit text-open-line
    textedit text-forward-char
    textedit text-get-cursor-position __current_cursor
    textedit text-end
    textedit text-get-cursor-position __old_end_of_file
    compcmd include-message
    if $status != 0
        textedit text-set-cursor-position $__current_cursor
        textedit text-delete-backward-char
        return
    endif
    textedit text-set-cursor-position $__old_end_of_file
    textedit text-start-selecting
    textedit text-end
    textedit text-cut-selection
    textedit text-deselect
    textedit text-set-cursor-position $__current_cursor
    textedit text-paste
}
function zbutton_read() {
    msg_list | set __msg_list
    if "x$__msg_list" !~ "x*[-,]*"
        type
    else
        error -p $__err_read_multiple
    endif
}
function zbutton_reuse() {
    zmenu_set_tmpdir
    textedit set-item compose-body
    textedit save-to-file $__tmpdir/zmsg$$
    if $status != 0
        return
    endif
    textedit set-item subject-header-field
    textedit get-text __subj
    # if the above operation did not succeed, Edit Headers must be set
    if $status == 0
        if "x$__subj" == "x"
            mail -edit_hdrs! -file $__tmpdir/zmsg$$
        else
            mail -edit_hdrs! -subject "$__subj" -file $__tmpdir/zmsg$$
        endif
    else
        mail -edit_hdrs -draft $__tmpdir/zmsg$$
    endif
    remove -f $__tmpdir/zmsg$$
}
function zmenu_edit_signature() {
    set __sigstrname = ~/.sigstring
    set __sigfilename = ~/.signature
    if $?autosign
        if "X$autosign" =~ 'X\\*'
            sh echo "$autosign" > $__sigstrname
            set autosign = $__sigstrname
        else
            if "X$autosign" =~ 'X\[*\]'
                sh echo "$autosign" > $__sigstrname
                set autosign = $__sigstrname
            endif
        endif
    endif
    if ! $?autosign
        set autosign = $__sigfilename
    endif
    if X"$autosign" == X
        set __sigfile = $__sigfilename
    else
        set __sigfile = "$autosign"
    endif
    if ! -e $__sigfile
        if $?realname
            sh echo "$realname" > "$__sigfile"
        else
            sh echo > "$__sigfile"
        endif
    endif
    page -e "$__sigfile"
    unset __sigfile __sigstrname __sigfilename
}
function zmenu_view_only_selected() {
    msg_list | set __mlist
    flags +H *{$__mlist}
    unset __mlist
    msg_list .
}
function zmenu_unhide_all() {
    flags -H *
    unset hidden
    msg_list .
}
function zmenu_paste_indented() {
    set __pfx=''
    if $?indent_str != 0
        set __pfx="$indent_str"
    endif
    if "x$__pfx" == "x"
        ask -i __pfx "$__ask_indent_with"
    else
        ask -i __pfx -d "$__pfx" "$__ask_indent_with"
    endif
    if $status == 0
        textedit set-item compose-body
        textedit get-cursor-position __indentstart
        textedit paste
        textedit get-cursor-position __indentend
        if x$__indentstart == x$__indentend
            error -p "$__err_clipboard_empty"
        else
            textedit set-selection-position $__indentstart $__indentend
            textedit indent "$__pfx"
        endif
    endif
    unset __pfx
}
function zmenu_paste_fill() {
    textedit set-item compose-body
    textedit get-cursor-position __indentstart
    textedit paste
    textedit get-cursor-position __indentend
    if x$__indentstart == x$__indentend
        error -p "$__err_clipboard_empty"
    else
        textedit set-selection-position $__indentstart $__indentend
        textedit fill
    endif
}
function zmenu_remove_folder() {
    ask -f __file -d +/ "$__ask_folder_remove"
    if $status == 0
        rmfolder "$__file"
        unset __file
    endif
}
function zmenu_merge_folder() {
    ask -f __file -d +/ "$__ask_folder_merge"
    if $status == 0
        merge "$__file"
        unset __file
    endif
}
function zmenu_sort_by() {
    sort $*
    if $?main_state:(folder_has_messages)
        redraw .
    endif
}
function zmenu_full_headers() {
    if ! $?display_headers
        set display_headers = unignored
    endif
    display
}
function zmenu_load_main_toolbar() {
       if $?main_toolbar_loaded
           return
       else
           if ! $?main_panes:(toolbar)
               return
           endif
       endif
    
       set __icons = $ZMLIB/bitmaps/toolbar
    
    # main window toolbar items
    button -B MainToolbar
    button -n -name Open -label 'Open' -icon $__icons/open.xbm dialog AddFolder
    button -name Save -label Save -icon $__icons/save.xbm dialog Save
    button -label Read -icon $__icons/read.xbm zbutton_read
    button -label Delete -icon $__icons/delete.xbm delete
    button -label Undelete -icon $__icons/undelete.xbm undelete
    button -label Print -icon $__icons/printer.xbm dialog Printer
    button -n -name Create -label 'Create New' -icon $__icons/compose.xbm mail
    #button -label Reply -sensitivity '$?main_state:(folder_has_messages)' -icon $__icons/reply.xbm reply
    button -label Reply -icon $__icons/reply.xbm reply
    #button -name ReplyAll -label 'Reply All' -sensitivity '$?main_state:(folder_has_messages)' -icon $__icons/replyall.xbm replyall
    button -name ReplyAll -label 'Reply All' -icon $__icons/replyall.xbm replyall
    button -n -label Update -icon $__icons/update.xbm update
    
    button -W main
    
       set main_toolbar_loaded
       unset __icons
}
function zmenu_load_message_toolbar() {
       if $?message_toolbar_loaded
           return
       else
           if ! $?message_panes:(toolbar)
               return
           endif
       endif
    
       set __icons = $ZMLIB/bitmaps/toolbar
    
    # message window toolbar items
    button -B MessageToolbar
    button -n -label Close -icon $__icons/close.xbm dialog -close
    button -n -label Save -icon $__icons/save.xbm dialog Save
    button -n -label Print -icon $__icons/printer.xbm dialog Printer
    button -n -label 'Create New' -icon $__icons/compose.xbm mail
    button -n -label Reply -icon $__icons/reply.xbm reply
    button -n -name All -label 'Reply All' -icon $__icons/replyall.xbm replyall
    button -n -label Forward -icon $__icons/fwd.xbm mail -f
    button -n -label Attach -icon $__icons/attach.xbm dialog Attachments
    #button -n -sensitivity '$?message_state:(is_prev) && !$?message_state:(pinup)' -label Previous -icon $__icons/prev.xbm previous
    button -n -label Previous -icon $__icons/prev.xbm previous
    #button -n -sensitivity '$?message_state:(is_next) && !$?message_state:(pinup)' -label Next -icon $__icons/next.xbm next
    button -n -label Next -icon $__icons/next.xbm next
    button -n -label Delete -icon $__icons/delete.xbm delete
    
    button -W main
    
       set message_toolbar_loaded
       unset __icons
}
function zmenu_load_compose_toolbar() {
       if $?compose_toolbar_loaded
           return
       else
           if ! $?compose_panes:(toolbar)
               return
           endif
       endif
    
       set __icons = $ZMLIB/bitmaps/toolbar
    
    # compose window toolbar items
    button -B ComposeToolbar
    button -n -label Close -focus-condition '!$?compose_state:(active)' -icon $__icons/close.xbm 'zmenu_comp_done'
    button -n -label Cancel -icon $__icons/cancel.xbm compcmd cancel
    button -n -label Cut -icon $__icons/cut.xbm zmenu_comp_body_textcopy cut-selection
    button -n -label Copy -icon $__icons/copy.xbm zmenu_comp_body_textcopy copy-selection
    button -n -label Paste -icon $__icons/paste.xbm zmenu_comp_body_textedit paste
    button -n -label Search -icon $__icons/search.xbm dialog SearchReplace
    button -n -label Aliases -icon $__icons/alias.xbm dialog CompAliases
    button -n -label Address -icon $__icons/addrbook.xbm dialog Browser
    #button -n -sensitivity '$?main_state:(folder_has_messages)' -label Include -icon $__icons/include.xbm zmenu_toolbar_include
    button -n -label Include -icon $__icons/include.xbm zmenu_toolbar_include
    button -n -label Attach -icon $__icons/attach.xbm dialog Attachments
    button -n -label Send -icon $__icons/send.xbm zmenu_toolbar_send
    
    button -W main
    
       set compose_toolbar_loaded
       unset __icons
}
#
# User-Defined Buttons
#
button -window main -b MainActions32
button -window message -b MessageActions32
button -window compose -b ComposeActions32

button -B MainActions32
button -name Read "zbutton_read"
button -name Delete "delete"
button -name Undelete "undelete"
button -name Save "dialog Save"
button -name Print "lpr"
button -no-msgs -name Compose "mail"
button -name Reply "replysender"
button -name Forward "mail -f"
button -no-msgs -name Update "update"

button -B MessageActions32
button -window message -name Done "dialog -close"
button -window message -sensitivity '$?message_state:(is_next) && !$?message_state:(pinup)' -name Next "next"
button -window message -sensitivity '$?message_state:(is_prev) && !$?message_state:(pinup)' -name Prev "previous"
button -window message -name Delete "delete"
button -window message -name Save "dialog Save"
button -window message -name Reply "reply"
button -window message -name ReplyAll "replyall"

button -B ComposeActions32
button -window compose -name Done "zmenu_comp_done"
button -window compose -sensitivity '$?compose_state:(active)' -name Send "compcmd send"
button -window compose -sensitivity '$?compose_state:(active) && $?main_state:(folder_has_messages)' -name Include "compcmd include-message"
button -window compose -sensitivity '$?compose_state:(active)' -name Attachments "dialog Attachments"
button -window compose -sensitivity '$?compose_state:(active)' -name Cancel "compcmd cancel"

#
# User-Defined Menus
#

menu -B MainFolderMenu32
menu -no-msgs -name mfm_new_folder "dialog NewFolder"
menu -no-msgs -name mfm_open_folder "dialog AddFolder"
menu -no-msgs -name mfm_rename_folder "dialog RenameFolder"
menu -no-msgs -name mfm_remove_folder "zmenu_remove_folder"
menu -no-msgs -name mfm_update "update"
menu -no-msgs -name mfm_close_fldr "close"
menu -separator -name fmsep
menu -no-msgs -name mfm_quit "quit"

menu -B PriorityMenu32
menu -name emp_low "mark -Low"
menu -name emp_urgent "mark -Medium"
menu -name emp_high "mark -High"
menu -name emp_none "mark -"
menu -separator -name _sep1
menu -name emp_other "zmenu_msg_pri_other"

menu -B MainMessageMenu32
menu -name mm_read "display"
menu -name mm_save "dialog Save"
menu -name mm_delete "delete"
menu -name mm_undelete "undelete"
menu -name mm_print "dialog Printer"
menu -name mm_pinup "pinup"
menu -separator -name __sep0
menu -menu PriorityMenu32 -name mm_priority
menu -name mm_mark "mark"
menu -name mm_unmark "unmark"
menu -name mm_preserve "preserve"
menu -name mm_unpreserve "unpreserve"
menu -separator -name _sep1
menu -no-msgs -name mm_select_all "msg_list *"

menu -B MainViewMenu32
menu -no-msgs -name vm_view_all "zmenu_unhide_all"
menu -no-msgs -name vm_view_sel_only "zmenu_view_only_selected"
menu -name vm_hide_sel "hide"
menu -separator -name _sep0
menu -no-msgs -value '$?hidden:(old)' -name vm_hide_old
menu -no-msgs -value '$?hidden:(deleted)' -name vm_hide_del

menu -B FindAllRefMenu32
menu -name arm_same_subject "zmenu_all_ref_subj"
menu -name arm_same_author "zmenu_all_ref_auth"
menu -name arm_same_msgid "zmenu_all_ref_msgid"
menu -name arm_same_priority "zmenu_all_ref_priority"
menu -name arm_same_mark "zmenu_all_ref_mark"

menu -B FindNextRefMenu32
menu -name nrm_same_subject "zmenu_next_ref_subj"
menu -name nrm_same_author "zmenu_next_ref_auth"
menu -name nrm_same_msgid "zmenu_next_ref_msgid"
menu -name nrm_same_priority "zmenu_next_ref_priority"
menu -name nrm_same_mark "zmenu_next_ref_mark"

menu -B FindPrevRefMenu32
menu -name prm_same_subject "zmenu_prev_ref_subj"
menu -name prm_same_author "zmenu_prev_ref_auth"
menu -name prm_same_msgid "zmenu_prev_ref_msgid"
menu -name prm_same_priority "zmenu_prev_ref_priority"
menu -name prm_same_mark "zmenu_prev_ref_mark"

menu -B MainFindMenu32
menu -menu FindAllRefMenu32 -name mfim_all_references
menu -menu FindNextRefMenu32 -name mfim_next_reference
menu -menu FindPrevRefMenu32 -name mfim_prev_reference
menu -separator -name _sep1
menu -no-msgs -name mfim_pattern "dialog Search"
menu -no-msgs -name mfim_date "dialog Dates"

menu -B MainSortMenu32
menu -no-msgs -name sm_date "zmenu_sort_by d"
menu -no-msgs -name sm_subject "zmenu_sort_by s"
menu -no-msgs -name sm_author "zmenu_sort_by a"
menu -no-msgs -name sm_length "zmenu_sort_by l"
menu -no-msgs -name sm_priority "zmenu_sort_by p"
menu -no-msgs -name sm_status "zmenu_sort_by S"
menu -separator -name _sep1
menu -no-msgs -name sm_custom "dialog Sort"

menu -B ReplyMenu32
menu -name rpm_replysender "replysender"
menu -name rpm_replyall "replyall"
menu -separator -name _sep0
menu -name rpm_sender_inc "replysender -i"
menu -name rpm_all_inc "replyall -i"

menu -B ForwardMenu32
menu -name fwdm_resend "mail -send -forward"
menu -name fwdm_forward "mail -f"
menu -name fwdm_forward_attach "mail -M"

menu -B MainComposeMenu32
menu -no-msgs -name mcm_compose "mail"
menu -separator -name _sep0
menu -menu ReplyMenu32 -sensitivity '$?main_state:(folder_has_messages)' -name mcm_reply
menu -menu ForwardMenu32 -name mcm_forward
menu -separator -name _sep1
menu -no-msgs -sensitivity '$?address_book' -name mcm_browser "dialog Browser"
menu -no-msgs -name mcm_templates "dialog Templates"

menu -B MainOptionsMenu32
menu -no-msgs -name mom_aliases "dialog Aliases"
menu -no-msgs -name mom_sig "zmenu_edit_signature"
menu -no-msgs -name mom_headers "dialog Headers"
menu -no-msgs -name mom_envelope "dialog Envelope"
menu -separator -name _sep0
menu -no-msgs -name mom_buttons "dialog Buttons"
menu -no-msgs -name mom_menus "dialog Menus"
menu -no-msgs -name mom_colors "dialog Colors"
menu -no-msgs -name mom_filters "dialog Filters"
menu -no-msgs -name mom_fonts "dialog Fonts"
menu -no-msgs -name mom_functions "dialog Functions"
menu -no-msgs -name mom_toolbox "dialog Toolbox"
menu -separator -name _sep1
menu -no-msgs -name mom_variables "dialog Variables"
menu -no-msgs -name mom_save_state "saveopts -g"

#menu -B MainLayoutMenu32
#menu -no-msgs -value '$?main_panes:(toolbar)' -name win_toolbar "zmenu_load_main_toolbar"
#menu -no-msgs -value '$?main_panes:(status)' -name win_title
#menu -no-msgs -value '$?main_panes:(folder)' -name win_control
#menu -no-msgs -value '$?main_panes:(messages)' -name win_list
#menu -no-msgs -value '$?main_panes:(buttons)' -name win_panel
#menu -no-msgs -value '$?main_panes:(output)' -name win_output
#menu -no-msgs -value '$?main_panes:(command)' -name win_command
#menu -no-msgs -value '$?main_panes:(status_bar)' -name win_status

#menu -B MainHelpMenu32
#menu -no-msgs -name mhm_context "help -c"
#menu -no-msgs -name mhm_general "help -i General"
#menu -separator -name _sep0
#menu -no-msgs -name mhm_menus "help -i Main Window"
#menu -no-msgs -name mhm_compose "help -i Compose Window"
#menu -no-msgs -name mhm_read "help -i Reading Messages"
#menu -no-msgs -name mhm_print "help -i Printing Messages"
#menu -no-msgs -name mhm_reply "help -i Replying to a Message"
#menu -no-msgs -name mhm_saving "help -i Saving Messages"
#menu -no-msgs -name mhm_updating "help -i Updating"
#menu -no-msgs -name mhm_opening "help -i Opening Folders"
#menu -no-msgs -name mhm_indexing "help -i Folder Popup"
#menu -separator -name _sep1
#menu -no-msgs -name mhm_functions "dialog functionsHelp"
#menu -no-msgs -name mhm_index "help -t -i Main Window"
#menu -separator -name _sep2
#menu -no-msgs -name mhm_product "help -i About This Program"

menu -B MessageMessageMenu32
menu -name mmm_save_message "dialog Save"
menu -name mmm_delete "delete"
menu -name mmm_undelete "undelete"
menu -name mmm_print "dialog Printer"
menu -name mmm_pinup "pinup"
menu -separator -name _sep0
menu -menu PriorityMenu32 -name mmm_priority
menu -name mmm_mark "mark"
menu -name mmm_unmark "unmark"
menu -name mmm_preserve "preserve"
menu -name mmm_unpreserve "unpreserve"
menu -separator -name _sep1
menu -sensitivity '$?message_state:(attachments)' -name mmm_attachments "dialog Attachments"
menu -separator -name _sep2
menu -no-msgs -name mmm_done "dialog -close"

menu -B MessageEditMenu32
menu -no-msgs -name em_copy "zmenu_msg_body_textcopy copy-selection"
menu -no-msgs -name em_select_all "zmenu_msg_body_textedit select-all"
menu -separator -name _sep1
menu -no-msgs -name em_next_page_next "zmenu_msg_body_textedit next-page"
menu -no-msgs -name em_prev_page "zmenu_msg_body_textedit previous-page"
menu -no-msgs -name em_scroll_up "zmenu_msg_body_textedit scroll-down"
menu -no-msgs -name em_scroll_down "zmenu_msg_body_textedit scroll-up"
menu -no-msgs -name em_scroll_home "zmenu_msg_body_textedit beginning"
menu -no-msgs -name em_scroll_end "zmenu_msg_body_textedit end"

menu -B MessageFindMenu32
menu -name fm_next "next"
menu -name fm_prev "previous"
menu -separator -name _sep0
menu -menu FindNextRefMenu32 -name fm_next_reference
menu -menu FindPrevRefMenu32 -name fm_prev_reference
menu -separator -name _sep1
menu -no-msgs -name fm_search "dialog SearchReplace"

menu -no-msgs -b MessageComposeMenu32 -name cm_compose "mail"
menu -no-msgs -b MessageComposeMenu32 -name cm_templates "dialog Templates"

#menu -B MessageLayoutMenu32
#menu -no-msgs -value '$?message_panes:(toolbar)' -name lm_toolbar "zmenu_load_message_toolbar"
#menu -no-msgs -value '$?message_panes:(folder)' -name lm_folder
#menu -no-msgs -value '$?message_panes:(headers)' -name lm_header
#menu -no-msgs -sensitivity '$?message_state:(attachments) && $?message_panes:(headers)' -value '$?message_panes:(attachments)' -name lm_attachments
#menu -no-msgs -value '$?message_panes:(body)' -name lm_body
#menu -no-msgs -value '$?message_panes:(action_area)' -name lm_actions
#menu -no-msgs -value '$?message_panes:(status_bar)' -name lm_status
#menu -separator -name _sep0
#menu -sensitivity '!$?message_state:(pinup)' -value '$?display_headers:(all)' -name lm_full_headers "zmenu_full_headers"

#menu -B MessageHelpMenu32
#menu -name msghm_context "help -c"
#menu -separator -name _sep0
#menu -no-msgs -name msghm_about "help -i Message Display Window"
#menu -no-msgs -name msghm_displaying "help -i Receiving Attachments"
#menu -no-msgs -name msghm_replying "help -i Replying to a Message"
#menu -no-msgs -name msghm_print "help -i Printing Messages"
#menu -no-msgs -name msghm_saving "help -i Saving Messages"
#menu -separator -name _sep1
#menu -no-msgs -name msghm_index "help -t -i Message Display Window"

menu -B ComposeMessageMenu32
menu -no-msgs -name mm_compose "mail"
menu -sensitivity '!$?compose_state:(active)' -name mm_reuse "zbutton_reuse"
menu -no-msgs -sensitivity '!$?compose_state:(active)' -name mm_templates "dialog Templates"
menu -separator -name _sep0
menu -no-msgs -sensitivity '$?compose_state:(active)' -name mm_send "compcmd send"
menu -no-msgs -sensitivity '$?compose_state:(active)' -name mm_save "zmenu_save_to_file"
menu -no-msgs -sensitivity '$?compose_state:(active)' -name mm_draft "zmenu_save_draft"
menu -no-msgs -sensitivity '$?compose_state:(active)' -name mm_print "zmenu_comp_print"
menu -separator -name _sep1
menu -sensitivity '$?compose_state:(active)' -name mmm_attachments "dialog Attachments"
menu -separator -name _sep2
menu -no-msgs -sensitivity '$?compose_state:(active)' -name mm_cancel "compcmd quit"
menu -no-msgs -name mm_done "zmenu_comp_done"

menu -B ComposePasteSpecialMenu32
menu -no-msgs -name psm_indent "zmenu_paste_indented"
menu -no-msgs -name psm_fill "zmenu_paste_fill"

menu -B ComposeEditFormatMenu32
menu -no-msgs -name em_fill "zmenu_comp_body_textedit fill"
menu -no-msgs -name em_indent "zmenu_comp_indent"
menu -no-msgs -name em_unindent "zmenu_comp_body_textedit unindent"
menu -no-msgs -name em_process "zmenu_comp_pipe"

menu -B ComposeEditMenu32
menu -no-msgs -name em_cut "zmenu_comp_body_textcopy cut-selection"
menu -no-msgs -name em_copy "zmenu_comp_body_textcopy copy-selection"
menu -no-msgs -name em_paste "zmenu_comp_body_textedit paste"
menu -menu ComposePasteSpecialMenu32 -name em_paste_sp
menu -no-msgs -name em_delete "zmenu_comp_body_textcopy clear-selection"
menu -menu ComposeEditFormatMenu32 -name em_format
menu -separator -name _sep0
menu -no-msgs -name em_select_all "zmenu_comp_body_textedit select-all"
menu -separator -name _sep1
menu -no-msgs -name em_search "dialog SearchReplace"
menu -separator -name _sep2
menu -no-msgs -name em_editor "compcmd edit"

menu -B ComposeIncludeMessageMenu32
menu -name imm_selected "compcmd include-message"
menu -name imm_forward "compcmd forward-unindented"
menu -name imm_forward_attach "compcmd forward-attached"

menu -no-msgs -b ComposeIncludeFileMenu32 -name ifm_indented "zmenu_insert_file insert"
menu -no-msgs -b ComposeIncludeFileMenu32 -name ifm_indentedr "zmenu_insert_file replace"

menu -b ComposeIncludeMenu32 -menu ComposeIncludeMessageMenu32 -sensitivity '$?main_state:(folder_has_messages)' -name im_messages
menu -b ComposeIncludeMenu32 -menu ComposeIncludeFileMenu32 -name im_files

menu -B ComposeAddressesMenu32
menu -no-msgs -name hdrs_aliases "dialog CompAliases"
menu -no-msgs -sensitivity '$?address_book' -name hdrs_browse "dialog Browser"
menu -no-msgs -sensitivity '$?address_book' -name hdrs_check "compcmd address-check"
menu -no-msgs -name hdrs_dynamic "dialog DynamicHdrs"
menu -separator -name _sep0
menu -no-msgs -name hdrs_to "compcmd to"
menu -no-msgs -name hdrs_subject "compcmd subject"
menu -no-msgs -name hdrs_cc "compcmd cc"
menu -no-msgs -name hdrs_bcc "compcmd bcc"

menu -B ComposePriorityMenu32
menu -no-msgs -value '$?compose_state:(pri_Low)' -name emp_low
menu -no-msgs -value '$?compose_state:(pri_Medium)' -name emp_urgent
menu -no-msgs -value '$?compose_state:(pri_High)' -name emp_high
menu -no-msgs -value '$?compose_state:(pri_none)' -name emp_none
menu -no-msgs -separator -name _sep1
menu -no-msgs -name emp_other "zmenu_comp_pri_other"

menu -B ComposeOptionsMenu32
menu -no-msgs -sensitivity '$?address_book || $?compose_state:(directory_check)' -value '$?compose_state:(directory_check)' -name com_directory
menu -no-msgs -value '$?compose_state:(autosign)' -name com_autosign
menu -no-msgs -value '$?compose_state:(autoformat)' -name com_autoformat
menu -no-msgs -value '$?compose_state:(edit_headers)' -name com_edit-headers
menu -menu ComposePriorityMenu32 -name hdrs_priority
menu -no-msgs -sensitivity '$?address_book' -value '$?compose_state:(sendtime_check)' -name com_sendcheck
menu -no-msgs -value '$?compose_state:(return_receipt)' -name com_return-receipt
menu -no-msgs -value '$?compose_state:(record_msg)' -name com_record-msg
menu -separator -name _sep0
menu -name com_options "dialog CompOptions"

#menu -B ComposeLayoutMenu32
#menu -no-msgs -sensitivity '$?compose_state:(active) || $?compose_panes:(toolbar)' -value '$?compose_panes:(toolbar)' -name lm_toolbar "zmenu_load_compose_toolbar"
#menu -no-msgs -value '$?compose_panes:(folder)' -name lm_folder
#menu -no-msgs -value '!$?compose_state:(edit_headers)' -name lm_hdrs
#menu -no-msgs -sensitivity '$?compose_state:(attachments)' -value '$?compose_panes:(attachments)' -name lm_attachments
#menu -no-msgs -value '$?compose_panes:(body)' -name lm_body
#menu -no-msgs -value '$?compose_panes:(action_area)' -name lm_actions
#menu -no-msgs -value '$?compose_panes:(status_bar)' -name lm_status

#menu -B ComposeHelpMenu32
#menu -name chm_context "help -c"
#menu -separator -name _sep0
#menu -no-msgs -name chm_about "help -i Compose Window"
#menu -no-msgs -name chm_addressing "help -i Addressing"
#menu -no-msgs -name chm_add_attach "help -i Sending Attachments"
#menu -no-msgs -name chm_include "help -i Including"
#menu -no-msgs -name chm_send "help -i Sending a message"
#menu -no-msgs -name chm_alias "help -i Using Aliases"
#menu -no-msgs -name chm_options "help -i Compose Options"
#menu -separator -name _sep1
#menu -no-msgs -name chm_index "help -t -i Compose Window"

#
# Install the menus
#

menu -W main -b MainMenu32
menu -menu MainFolderMenu32 -name folder
menu -menu MainMessageMenu32 -name message
menu -menu MainViewMenu32 -name view
menu -menu MainFindMenu32 -name find
menu -menu MainSortMenu32 -name sort
menu -menu MainComposeMenu32 -name compose
menu -menu MainOptionsMenu32 -name options
#menu -menu MainLayoutMenu32 -name layout
menu -menu MainLayoutMenu -name layout
#menu -menu MainHelpMenu32 -help-menu -name help
menu -menu MainHelpMenu -help-menu -name help

menu -W message -b MessageMenu32
menu -window message -menu MessageMessageMenu32 -name message
menu -window message -menu MessageEditMenu32 -name edit
menu -window message -menu MessageFindMenu32 -name find
menu -window message -menu MessageComposeMenu32 -name compose
menu -window message -menu ReplyMenu32 -name reply
menu -window message -menu ForwardMenu32 -name forward
#menu -window message -menu MessageLayoutMenu32 -name layout
menu -window message -menu MessageLayoutMenu -name layout
#menu -window message -menu MessageHelpMenu32 -help-menu -name help
menu -window message -menu MessageHelpMenu -help-menu -name help

menu -W compose -b ComposeMenu32
menu -window compose -menu ComposeMessageMenu32 -name message
menu -window compose -menu ComposeEditMenu32 -sensitivity '$?compose_state:(active)' -name edit
menu -window compose -menu ComposeIncludeMenu32 -sensitivity '$?compose_state:(active)' -name include
menu -window compose -menu ComposeAddressesMenu32 -sensitivity '$?compose_state:(active)' -name address
menu -window compose -menu ComposeOptionsMenu32 -sensitivity '$?compose_state:(active)' -name options
#menu -window compose -menu ComposeLayoutMenu32 -name layout
menu -window compose -menu ComposeLayoutMenu -name layout
#menu -window compose -menu ComposeHelpMenu32 -help-menu -name help
menu -window compose -menu ComposeHelpMenu -help-menu -name help

menu -popup-context main-summaries -b MainSummariesPopupMenu32
menu -popup main-summaries -name msp_read "type"
menu -popup main-summaries -name msp_delete "delete"
menu -popup main-summaries -name msp_undelete "undelete"
menu -popup main-summaries -name msp_save "dialog Save"
menu -popup main-summaries -name msp_hide "flags +H"
menu -popup main-summaries -menu PriorityMenu32 -name msp_pri
menu -popup main-summaries -name msp_lpr "lpr"
menu -no-msgs -popup main-summaries -name msp_select_all "msg_list *"

#
# Functions to swap them in and out
#

function zmenu_three_two() {
    button -W main -b MainActions32
    button -W compose -b ComposeActions32
    button -W message -b MessageActions32
    menu -W main -b MainMenu32
    menu -W message -b MessageMenu32
    menu -W compose -b ComposeMenu32
    menu -popup-context main-summaries -b MainSummariesPopupMenu32
}

function zmenu_restore_defaults() {
    button -W main -b MainActions
    button -W compose -b ComposeActions
    button -W message -b MessageActions
    menu -W main -b MainMenu
    menu -W compose -b ComposeMenu
    menu -W message -b MessageMenu
    menu -popup main-summaries -b MainSummariesPopupMenu
}
